import request from './request'
import { ElMessage } from 'element-plus'

// 分页获取音乐列表
export const getMusicPage = (page = 1, size = 10) => {
  return request.get('/music', {
    params: { page, size },
  })
}

// 通过ID获取音乐
export const getMusicById = (id) => {
  return request.get(`/music/${id}`)
}

// 通过用户ID获取音乐
export const getMusicByUserId = async (userId) => {
  try {
    const response = await request({
      url: `/music/user/${userId}`,
      method: 'GET',
    })
    return response
  } catch (error) {
    ElMessage.error('获取用户音乐失败')
    return { success: false, message: error.message }
  }
}

// 通过曲风ID获取音乐列表
export const getMusicByGenre = (genreId) => {
  return request.get(`/music/by-genre/${genreId}`)
}

// 通过曲风ID分页获取音乐列表
export const getMusicByGenrePage = (genreId, page = 1, size = 12) => {
  return request.get(`/music/by-genre/${genreId}/page`, {
    params: { page, size }
  })
}

// 更新音乐
export const updateMusic = (musicData) => {
  return request.put('/music', musicData)
}

// 删除音乐
export const deleteMusic = (id) => {
  return request.delete(`/music/${id}`)
}

// 上传音乐
export const uploadMusic = (formData, onUploadProgress) => {
  return request.post('/music/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    onUploadProgress: onUploadProgress,
  })
}

// 添加音乐到专辑
export const addMusicToAlbum = (albumId, musicId) => {
  return request.post(`/album/${albumId}/add-music/${musicId}`)
}

// 获取专辑列表
export const getAlbums = (page = 1, size = 10) => {
  return request.get('/album/page', { params: { page, size } })
}

// 通过用户ID获取专辑
export const getAlbumsByUserId = async (userId) => {
  try {
    const response = await request({
      url: `/album/user/${userId}`,
      method: 'GET',
    })
    return response
  } catch (error) {
    ElMessage.error('获取用户专辑失败')
    return { success: false, message: error.message }
  }
}

// 获取曲风列表
export const getGenres = () => {
  return request.get('/genre/tree')
}

// 如需搜索曲风可使用以下方法
// export const searchGenres = (keyword) => request.get('/genre/search', { params: { keyword } });
// 如需分页搜索曲风可使用以下方法
// export const searchGenresPage = (page = 1, size = 10, keyword = '') => request.get('/genre/search/page', { params: { page, size, keyword } });
